/**
 * 发送ajax请求
 */
export const http = ({
	url="",
	method="GET",
	data={},
	header={
		
	}
}) => {
	return new Promise((resolve,rejected) => {
		if(method.toUpperCase() === 'POST') {
			header["Content-Type"] ="application/x-www-form-urlencoded;charset=UTF-8"

		}
		uni.request({
			url,
			method,
			data,
			header,
			success(res) {
				resolve(res.data)
			},
			fail(err) {
				rejected(err)
			}
		})
	})
}

/**
 * 引入百度
 */
const initBaidu = () => {
	return new Promise((resolve,rejected) => {
		 window.init = function() {
			 resolve(initBaidu)
		 }
		 const scriptNode =  document.createElement('script')
		 scriptNode.src = "https://api.map.baidu.com/api?v=1.0&&type=webgl&ak=EJ05u3mmoakAcziuQSbSveiPyc08Qwlk&callback=init"
	     document.head.appendChild(scriptNode)
	})
}

/**
 * 获取当前地址
 */
export const getBaiDuLocation =  () => {
	
	return new Promise(async (resolve,reject) => {
		await initBaidu()
		 const map = new BMapGL.LocalCity()
			map.get(res => {
				 // console.log(res);
				 resolve(res)
				 
			})
		
	})
}

/**
 * 小程序获取经纬度
 */
export const getWeiXinLatLon =  () => {
	return new Promise((resolve,rejected) => {
		uni.getLocation({
			type: 'wgs84',
			success: function (res) {
				// console.log('当前位置的经度：' + res.longitude);
				// console.log('当前位置的纬度：' + res.latitude);
				
				resolve({
					lat:res.latitude,
					lon:res.longitude
				})
			},
			fail(err) {
				rejected(err)
			}
		})
	})
}

/**
 * 小程序获取地理信息
 */
export const getWeiXinLocation =  ({
	lat,
	lon
}) => {
	const QQMapWX = require('./qqmap-wx-jssdk.js');
	// 实例化API核心类
	const qqmapsdk = new QQMapWX({
		key: 'S5KBZ-7QT6J-U6ZFC-DDUSV-JOC42-G4BFM'
	});
	
	return new Promise((resolve,rejected) => {
		qqmapsdk.reverseGeocoder({
			location:`${lat},${lon}`,
			success(res) {
				//console.log(res);
				resolve(res)
			},
			fail(err) {
				// console.log(err);
				rejected(err)
			}
		})
	})
	
}